package com.rain.rainspringboot3.desensitize.core;

import com.rain.rainspringboot3.desensitize.core.base.handler.DesensitizationHandler;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class DesensitizationHandlerFactory {
    
    private final Map<Class<? extends DesensitizationHandler>, DesensitizationHandler> handlerCache = new ConcurrentHashMap<>();
    
    @SuppressWarnings("unused")
    public DesensitizationHandler getHandler(Class<? extends DesensitizationHandler> handlerClass) {
        return handlerCache.computeIfAbsent(handlerClass, this::createHandlerInstance);
    }
    
    private DesensitizationHandler createHandlerInstance(Class<? extends DesensitizationHandler> handlerClass) {
        try {
            return handlerClass.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException("创建脱敏处理器实例失败: " + handlerClass.getName(), e);
        }
    }
}
